package com.bobbygu.server.simpleapp.controller;

import com.bobbygu.server.simpleapp.mapper.UserMapper;
import com.bobbygu.server.simpleapp.model.UserModel;
import com.bobbygu.server.simpleapp.model.response.BaseResponse;
import com.bobbygu.server.simpleapp.model.response.ConstResponse;
import com.bobbygu.server.simpleapp.model.response.ResultResponse;
import com.bobbygu.server.simpleapp.model.response.StringResponse;
import com.bobbygu.server.simpleapp.utils.PatternUtil;
import com.bobbygu.server.simpleapp.utils.SafeUtil;
import com.bobbygu.server.simpleapp.utils.TextUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;
import java.util.logging.Logger;

@RestController
public class UserController {
    @SuppressWarnings("SpringJavaAutowiringInspection")
    @Autowired
    private UserMapper userMapper;

    @RequestMapping(value = "/home/login", method = RequestMethod.POST)
    public BaseResponse login(@RequestBody Map<String, Object> requestMap) {
        System.out.println("login");
        String name = requestMap.get("Name").toString();
        String pass = requestMap.get("Pass").toString();
        System.out.println("name=" + name + " pass=" + pass);
        if (name == null || pass == null) {
            return new BaseResponse(ConstResponse.DESC_PARAM, ConstResponse.STATUS_PARAM_ERROR);
        }
        UserModel userModel = userMapper.findUserByname(name);
        if (userModel == null) {
            return new BaseResponse("用户名不存在", ConstResponse.STATUS_KNOWN_ERROR);
        }
        pass = SafeUtil.shortMD5(pass);
        if (!userModel.password.equals(pass)) {
            return new BaseResponse("密码输入错误", ConstResponse.STATUS_KNOWN_ERROR);
        }
        return new ResultResponse(userModel, "登录成功", ConstResponse.STATUS_OK);
    }

    @RequestMapping(value = "/home/register", method = {RequestMethod.POST})
    public BaseResponse register(@RequestBody Map<String, Object> requestMap) {
        System.out.println("register");
        if (requestMap.get("Name") == null) {
            return new BaseResponse("Name 不能为空", ConstResponse.STATUS_PARAM_ERROR);
        }
        if (requestMap.get("Pass") == null) {
            return new BaseResponse("Pass 不能为空", ConstResponse.STATUS_PARAM_ERROR);
        }
        String name = requestMap.get("Name").toString();
        String password = requestMap.get("Pass").toString();
        System.out.println("phone=" + name + " password=" + password);
        if (name == null || password == null) {
            return new BaseResponse(ConstResponse.DESC_PARAM, ConstResponse.STATUS_PARAM_ERROR);
        }

        String nameCheck = PatternUtil.isNickname(name);
        if (!TextUtils.isEmpty(nameCheck)) {
            return new BaseResponse(nameCheck, ConstResponse.STATUS_KNOWN_ERROR);
        }
        if (!PatternUtil.isPassword(password)) {
            return new BaseResponse("请输入6~18位密码", ConstResponse.STATUS_KNOWN_ERROR);
        }
        UserModel userfind = userMapper.findUserByname(name);
        if (userfind != null) {
            return new BaseResponse("用户已注册", ConstResponse.STATUS_KNOWN_ERROR);
        }
        String token = SafeUtil.MD5(name + "app" + password);
        password = SafeUtil.shortMD5(password);
        userMapper.register(name, password, token);
        return new ResultResponse(new StringResponse("注册成功"), ConstResponse.DESC_OK, ConstResponse.STATUS_OK);
    }
}
